<?php require('connections/con_gl.php'); ?>
<?php
cekAkses($_SESSION[akses],"L-6");
if($_GET['act']=='periode') {
$colname_xyz = "-1";
if (isset($_POST['cr_per'])) {
  $colname_xyz = (get_magic_quotes_gpc()) ? $_POST['cr_per'] : addslashes($_POST['cr_per']);
}
mysql_select_db($database_con_gl, $con_gl);
$query_xyz = sprintf("SELECT awal, akhir FROM gl_periode WHERE id = %s", $colname_xyz);
$xyz = mysql_query($query_xyz, $con_gl) or die(mysql_error());
$row_xyz = mysql_fetch_assoc($xyz);
$totalRows_xyz = mysql_num_rows($xyz);
$mulai = $row_xyz['awal'];
$sampai = $row_xyz['akhir']; 
}else{
$mulai = $_POST['Mulai'];
$sampai = $_POST['Sampai'];
}
$currentPage = $_SERVER["PHP_SELF"];

/*$maxRows_data = 30;
$pageNum_data = 0;
if (isset($_GET['pageNum_data'])) {
  $pageNum_data = $_GET['pageNum_data'];
}
$startRow_data = $pageNum_data * $maxRows_data;*/

mysql_select_db($database_con_gl, $con_gl);
$query_data =" SELECT * FROM gl_akun";
$jumcoa = count($_POST['akun']);
if($jumcoa > 0) {
$wherecoa = " where ";
for($ip=0;$ip<$jumcoa;$ip++) {
	if($_POST[akun][$ip] <>'') {
	$ipid = $_POST[akun][$ip];
	$wherecoa .=" id='$ipid'";
	$nextip = $ip + 1;
	if($_POST[akun][$nextip]<>'') {
	$wherecoa .=" OR ";
	}
	}
}
}
$query_data .=$wherecoa;
$query_data .=" ORDER by akun";
//$query_limit_data = sprintf("%s LIMIT %d, %d", $query_data, $startRow_data, $maxRows_data);
$data = mysql_query($query_data, $con_gl) or die(mysql_error());
$row_data = mysql_fetch_assoc($data);
$totalRows_data = mysql_num_rows($data);
/*if (isset($_GET['totalRows_data'])) {
  $totalRows_data = $_GET['totalRows_data'];
} else {
  $all_data = mysql_query($query_data);
  $totalRows_data = mysql_num_rows($all_data);
}
$totalPages_data = ceil($totalRows_data/$maxRows_data)-1;

$queryString_data = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_data") == false && 
        stristr($param, "totalRows_data") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_data = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_data = sprintf("&totalRows_data=%d%s", $totalRows_data, $queryString_data);*/

mysql_select_db($database_con_gl, $con_gl);
$query_period = "SELECT id, awal, akhir, saldoawal FROM gl_periode ORDER BY id ASC";
$period = mysql_query($query_period, $con_gl) or die(mysql_error());
$row_period = mysql_fetch_assoc($period);
$totalRows_period = mysql_num_rows($period);

mysql_select_db($database_con_gl, $con_gl);
$query_getklas = "SELECT kd, klasifikasi FROM gl_klas ORDER BY kd,klasifikasi ASC";
$getklas = mysql_query($query_getklas, $con_gl) or die(mysql_error());
$row_getklas = mysql_fetch_assoc($getklas);
$totalRows_getklas = mysql_num_rows($getklas);
include('include/widget_exportall.php');
?>
<link href="css/ui.datepicker.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="js/ui.datepicker.js"></script>
<script type="text/javascript">
<!--
$(function()
      {
        $('.calendar').datepicker({
            appendText : "",
            dateFormat : 'yy/mm/dd'
          });
      });
</script>
<link href="css/print.css" rel="stylesheet" type="text/css" media="print"/>
<h1 id="print">Daftar Buku Besar</h1>
<table width="100%" border="0" align="center" cellpadding="5" cellspacing="0" style="border-bottoms:solid 1px #E2EAC1;"> 
  <form id="search" name="search" method="post" action="#BukuBesar">
  <tr valign="top" id="print">
    <td colspan="3" align="right"><?php if(isset($_GET['act']) AND $_GET['act']=='periode') { ?>
            <label>
            <select name="cr_per" id="cr_per">
              <option value="0">Periode</option>
              <?php
do {  
?>
              <option value="<?php echo $row_period['id']?>" <?php if($row_period['id'] == $_POST['cr_per']) { ?>selected="selected"<?php } ?>><?php tanggal($row_period['awal'],"tampilkan");?> - <?php tanggal($row_period['akhir'],"tampilkan");?></option>
              <?php
} while ($row_period = mysql_fetch_assoc($period));
  $rows = mysql_num_rows($period);
  if($rows > 0) {
      mysql_data_seek($period, 0);
	  $row_period = mysql_fetch_assoc($period);
  }
?>
            </select>
			</label>
			<?php }else{ ?>
	<label>
			<input name="Mulai" type="text" class="calendar" id="Mulai" value="<?php if(empty($mulai)) echo "Mulai...";else echo $mulai;?>"onblur="if(this.value=='') this.value='Mulai...';" onFocus="if(this.value=='Mulai...') this.value='';"></label>
			s/d
			<label><input name="Sampai" type="text" class="calendar" id="Sampai" value="<?php if(empty($sampai)) echo "Sampai...";else echo $sampai;?>"onblur="if(this.value=='') this.value='Sampai...';" onFocus="if(this.value=='Sampai...') this.value='';"></label>
			<?php } ?>
			<label>
            <input name="Search" type="submit" id="Search" value="Tampilkan" />
            </label>        </td>
  </tr>
  <tr id="print">
    <td colspan="3"><table width="100%" border="0" cellspacing="5" cellpadding="2">
  <tr valign="top">
    <?php $q = 1; do { ?>
	<script type="text/javascript">
$(document).ready(function(){
$(".flip<?php echo $row_getklas['kd']; ?>").click(function(){
    $(".listCOA<?php echo $row_getklas['kd']; ?>").slideToggle("fast");
  });
});
</script>
	<?php
	mysql_select_db($database_con_gl, $con_gl);
$query_coa = "SELECT id, akun FROM gl_akun where klasifikasi ='$row_getklas[kd]' ORDER BY akun ASC";
$coa = mysql_query($query_coa, $con_gl) or die(mysql_error());
$row_coa = mysql_fetch_assoc($coa);
$totalRows_coa = mysql_num_rows($coa);
	?>
    <td width="33.3%" valign="top" style="border:solid 1px #999;padding:5px;background:#EEE;color:#2e6194;"><strong class="flip<?php echo $row_getklas['kd']; ?>" style="cursor:pointer;width:100%;displays:block;text-transform:uppercase;font-size:14px;" title="Click for view COA"><?php echo $row_getklas['klasifikasi'];?></strong> <br /><div align="right" style="margin:0;padding:0;">[ <span style="font-weight:normal;cursor:pointer;width:100%;displays:block;" class="flip<?php echo $row_getklas['kd']; ?>">Daftar Akun</span> ]</div><br /><ul style="list-style:none;padding:5px; margin:0;display:none;" class="listCOA<?php echo $row_getklas['kd']; ?>"><li style="font-weight:normal;"><table width="100%" border="0" cellspacing="0" cellpadding="2" align="center" class="datatable">
	  			<?php do { ?>
                <tr align="left" valign="top">
                  <td width="11%"><input name="akun[]" type="checkbox" id="akun[]" value="<?php echo $row_coa['id']; ?>" <?php $nilcoa="";$jumcoa = count($_POST[akun]);for($ii=0;$ii<$jumcoa;$ii++) { $nilcoa .= $_POST[akun][$ii];$nilcoa .="=,";?><?php if (!(strcmp($row_coa['id'],$_POST[akun][$ii]))) {echo "checked=\"checked\"";} ?><?php } ?>/></td>
                  <td width="89%"><span><?php echo $row_coa['id']; ?> - <?php echo $row_coa['akun']; ?></span></td>
                </tr>
				<?php } while ($row_coa = mysql_fetch_assoc($coa)); ?>
              </table></li></ul></td>
	<?php
	if($q % 3==0) { ?>
	</tr>
	<tr>
	<?php } $q++;?>
	<?php } while ($row_getklas = mysql_fetch_assoc($getklas)); ?>
</table>
    </td>
  </tr>
  </form>
  <?php if(isset($_POST['Search'])) { ?>
  <?php if($totalRows_data > 0) { ?>
  <?php do { ?>
  <?php
  $akun_id = $row_data['id'];
  mysql_select_db($database_con_gl, $con_gl);
$query_get_r ="SELECT no_ref, keterangan,tgl FROM gl_rtrans";
if($mulai <>'Mulai...' OR $sampai <>'Sampai...') {
$query_get_r .=" where tgl between '$mulai' AND '$sampai'";
}
$query_get_r .=" ORDER BY tgl asc";
$get_r = mysql_query($query_get_r, $con_gl) or die(mysql_error());
$row_get_r = mysql_fetch_assoc($get_r);
$totalRows_get_r = mysql_num_rows($get_r);

mysql_select_db($database_con_gl, $con_gl);
$query_saldo_deb = "SELECT sum(gl_trans.total) FROM gl_rtrans, gl_trans WHERE gl_trans.pos='D' AND gl_trans.no_ref=gl_rtrans.no_ref AND gl_trans.akun='$akun_id'";
if($_POST[Mulai]<>'Mulai...') {
$query_saldo_deb .=" AND gl_rtrans.tgl < '$_POST[Mulai]'";
}
$saldo_deb = mysql_query($query_saldo_deb, $con_gl) or die(mysql_error());
$row_saldo_deb = mysql_fetch_assoc($saldo_deb);
$totalRows_saldo_deb = mysql_num_rows($saldo_deb);

mysql_select_db($database_con_gl, $con_gl);
$query_saldo_kre = "SELECT sum(gl_trans.total) FROM gl_rtrans, gl_trans WHERE gl_trans.pos='K' AND gl_trans.no_ref=gl_rtrans.no_ref AND gl_trans.akun='$akun_id'";
if($_POST[Mulai]<>'Mulai...') {
$query_saldo_kre .=" AND gl_rtrans.tgl < '$_POST[Mulai]'";
}
$saldo_kre = mysql_query($query_saldo_kre, $con_gl) or die(mysql_error());
$row_saldo_kre = mysql_fetch_assoc($saldo_kre);
$totalRows_saldo_kre = mysql_num_rows($saldo_kre);
$saldoawalakun = $row_saldo_deb['sum(gl_trans.total)'] - $row_saldo_kre['sum(gl_trans.total)'];
  ?>
      <tr>
        <td colspan="3" align="center">&nbsp;</td>
      </tr>
      <tr>
    <td colspan="3" align="right" style="border-top:solid 1px #007EBF;"><strong style="color:#FFFFFF;padding:5px;position:relative;right:-5px;top:-5px;background:#007EBF;width:450px;display:block;text-align:center;"><?php echo $row_data['akun'];?></strong></td>
  </tr>
	<tr valign="top" bgcolor="#FFFFFF">
        <td colspan="3"  style="border-bottoms:solid 1px #E2EAC1;"><table width="100%" border="0" align="right" cellpadding="4" cellspacing="0" class="datatable">
          <tr>
            <th width="10%" align="center"><strong>Tanggal</strong></th>
            <th><strong>Transaksi</strong></th>
            <th width="15%" align="center" valign="top"><strong>Debet</strong></th>
            <th width="15%" align="center" valign="top"><strong>Kredit</strong></th>
            <th width="18%" align="center" valign="top"><strong>Balance</strong></th>
          </tr>
		  <tr valign="top" bgcolor="#FFFFFF">
                <td bgcolor="#FFFFFF">&nbsp;</td>
                <td bgcolor="#FFFFFF">Saldo Awal </td>
                <td align="right" valign="top" bgcolor="#FFFFFF">&nbsp;</td>
                <td align="right" valign="top" bgcolor="#FFFFFF">&nbsp;</td>
                <td align="right" valign="top" bgcolor="#FFFFFF"><?php if($row_data['pos']=='D') { echo number_format($row_data[saldoawal],0,',','.').",-";$totbal_x += $row_data[saldoawal];}else{ echo "( ".number_format(str_replace("-","",$row_data[saldoawal]),0,',','.').",-"." )";$totbal_x -= $row_data[saldoawal];}?></td>
          </tr>
		  <?php $totdeb=0; $totkre=0; $totbal_x = $row_data['saldoawal']; do { ?>
	<?php 
	mysql_select_db($database_con_gl, $con_gl);
$query_trans = "SELECT gl_rtrans.tgl, gl_trans.transaksi, gl_trans.id, gl_trans.total, gl_trans.pos, gl_akun.akun, gl_trans.no_ref FROM gl_trans, gl_rtrans, gl_akun WHERE gl_akun.id = gl_trans.akun AND gl_trans.no_ref='$row_get_r[no_ref]' AND gl_trans.akun='$akun_id' AND gl_trans.no_ref=gl_rtrans.no_ref ORDER BY gl_rtrans.tgl desc";
$trans = mysql_query($query_trans, $con_gl) or die(mysql_error());
$row_trans = mysql_fetch_assoc($trans);
$totalRows_trans = mysql_num_rows($trans);
	?>
		  <?php if($totalRows_trans > 0)  { ?>
		  <?php  do { ?>
              <tr valign="top" bgcolor="#FFFFFF">
              <td align="center" bgcolor="#FFFFFF"><?php tanggal($row_get_r['tgl'],"tampilkan");?></td>
            <td bgcolor="#FFFFFF"><a href="include/jurnalvoucher.php?referensi=<?php echo $row_trans[no_ref];?>" onClick="NewWindow(this.href,'name','940','300','yes');return false" title="Jurnal Voucher"><?php echo $row_trans['transaksi']; ?></a></td>
            <td align="right" valign="top" bgcolor="#FFFFFF"><?php if($row_trans['pos']=='D') { $totdeb += $row_trans['total']; echo number_format($row_trans['total'],0,',','.').",-"; if($row_data['pos']=='D') { $cur_totbal = $totbal_x + $row_trans['total'];}else{ $cur_totbal = $totbal_x - $row_trans['total'];}$totbal_x = $cur_totbal;}else{ echo "0,00";} ?></td>
            <td align="right" valign="top" bgcolor="#FFFFFF"><?php if($row_trans['pos']=='K') { $totkre += $row_trans['total']; echo number_format($row_trans['total'],0,',','.').",-";if($row_data['pos']=='K') { $cur_totbal = $totbal_x + $row_trans['total'];}else{ $cur_totbal = $totbal_x - $row_trans['total'];}$totbal_x = $cur_totbal;}else{ echo "0,00";} ?></td>
            <td align="right" valign="top" bgcolor="#FFFFFF"><?php if($totbal_x >0) { echo number_format($totbal_x,0,',','.').",-";}else{ echo "( ".number_format(str_replace("-","",$totbal_x),0,',','.').",-"." )";}?></td>
            </tr>
		  <?php } while ($row_trans = mysql_fetch_assoc($trans)); ?>
	  		
		  <?php } ?>
		  <?php } while ($row_get_r = mysql_fetch_assoc($get_r)); ?>
		  <tr valign="top">
		    <td colspan="2" align="right"><strong>Total : </strong></td>
                <td align="right" valign="top" bgcolor="#FFFFFF"><?php echo number_format($totdeb,0,',','.').",-";?></td>
                <td align="right" valign="top" bgcolor="#FFFFFF"><?php echo number_format($totkre,0,',','.').",-";?></td>
                <td align="right" valign="top" bgcolor="#FFFFFF"><?php if($totbal_x >0) { echo number_format($totbal_x,0,',','.').",-";}else{ echo "( ".number_format(str_replace("-","",$totbal_x),0,',','.').",-"." )";}?></td>
          </tr>
      </table></td>
    </tr>
      <?php } while ($row_data = mysql_fetch_assoc($data)); ?>
	<tr id="print">
    <td colspan="3"><a href="javascript:window.print();"><img src="images/_print.png" border="0" /> Cetak</a></td>
  </tr>
  <?php }else{ ?>
  <tr>
    <td colspan="3"  style="border-bottom:solid 1px #E2EAC1;border-right:solid 1px #E2EAC1;border-left:solid 1px #E2EAC1;">Data tidak ada !!! </td>
  </tr>
  <?php } ?>
  <?php } ?>
  <tr>
    <td colspan="3"><a name="BukuBesar" id="BukuBesar">&nbsp;</a></td>
  </tr>
</table>
